const p1 = new Promise(resolve => {
resolve(123);
})
const p2 = new Promise(resolve => {
resolve(456)
})
p1.then(p1Data => {
console.log(p1Data)
return p2
}).then(p2Data => {
console.log(p2Data)
})
.catch(err => console.log('err', err))
回傳 p1 & p2 的 resolve 結果!
const myPromise = new Promise( (resolve, reject) => {
var request = new XMLHttpRequest();
request.open(
'GET',
'https://run.mocky.io/v3/d2381372-4e30-4ec6-8a03-c3573417112d',
true
);
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
// Success!
var data = JSON.parse(this.response);
resolve(data)
} else {
reject('connect error!')
}
};
request.onerror = function(err) {
reject(err)
};
request.send();
})
myPromise.then(myPromise => {
console.log('myPromise data:', myPromise)
}).catch(err => console.log('err', err))
成功回傳該網址的 response!